
#include "Selector.h"

Selector::Selector(Scene^ project)
{
	this->actionNum = 0;
	this->linkNum = 0;
	this->source = project;
	this->SelectorAction = gcnew array<int>(project->getAMax());
	this->SelectorLink = gcnew array<int>(project->getLMax());
	for (int i=0;i<project->getAMax();i++) {this->SelectorAction[i]=0;}
	for (int i=0;i<project->getLMax();i++) {this->SelectorLink[i]=0;}

	this->SelectorUpLeft = gcnew array<System::Windows::Forms::PictureBox^>(project->getAMax()+project->getLMax());
	this->SelectorUpRight = gcnew array<System::Windows::Forms::PictureBox^>(project->getAMax()+project->getLMax());
	this->SelectorDownLeft = gcnew array<System::Windows::Forms::PictureBox^>(project->getAMax()+project->getLMax());
	this->SelectorDownRight = gcnew array<System::Windows::Forms::PictureBox^>(project->getAMax()+project->getLMax());
	for (int i=0;i<project->getAMax()+project->getLMax();i++) 
	{
		this->SelectorUpLeft[i] = gcnew System::Windows::Forms::PictureBox();
		this->SelectorUpLeft[i]->BackColor = System::Drawing::Color::Black;
		this->SelectorUpLeft[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorUpLeft[i]->Size = System::Drawing::Size(8, 8);
		this->SelectorUpLeft[i]->TabStop = false;
		this->SelectorUpLeft[i]->Cursor = System::Windows::Forms::Cursors::SizeNWSE;

		this->SelectorUpRight[i] = gcnew System::Windows::Forms::PictureBox();
		this->SelectorUpRight[i]->BackColor = System::Drawing::Color::Black;
		this->SelectorUpRight[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorUpRight[i]->Size = System::Drawing::Size(8, 8);
		this->SelectorUpRight[i]->TabStop = false;
		this->SelectorUpRight[i]->Cursor = System::Windows::Forms::Cursors::SizeNESW;

		this->SelectorDownLeft[i] = gcnew System::Windows::Forms::PictureBox();
		this->SelectorDownLeft[i]->BackColor = System::Drawing::Color::Black;
		this->SelectorDownLeft[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorDownLeft[i]->Size = System::Drawing::Size(8, 8);
		this->SelectorDownLeft[i]->TabStop = false;
		this->SelectorDownLeft[i]->Cursor = System::Windows::Forms::Cursors::SizeNESW;

		this->SelectorDownRight[i] = gcnew System::Windows::Forms::PictureBox();
		this->SelectorDownRight[i]->BackColor = System::Drawing::Color::Black;
		this->SelectorDownRight[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorDownRight[i]->Size = System::Drawing::Size(8, 8);
		this->SelectorDownRight[i]->TabStop = false;
		this->SelectorDownRight[i]->Cursor = System::Windows::Forms::Cursors::SizeNWSE;
	}
}

Selector^ Selector::MakeEqual(Selector^ other)
{
	this->actionNum = other->actionNum;
	this->linkNum = other->linkNum;
	this->source = other->source;
	for (int i=0;i<this->source->getAMax();i++) {this->SelectorAction[i]=other->SelectorAction[i];}
	for (int i=0;i<this->source->getLMax();i++) {this->SelectorLink[i]=other->SelectorLink[i];}
	return this;
}
int Selector::addAction(int n)
{
	this->SelectorAction[this->actionNum++] = n;
	if (this->actionNum >= this->source->getAMax())
	{
		return -1;
	}
	else
	{
		return 0;
	}
}
int Selector::addLink(int n)
{
	this->SelectorLink[this->linkNum++] = n;
	if (this->linkNum >= this->source->getLMax())
	{
		return -1;
	}
	else
	{
		return 0;
	}
}
void Selector::null()
{
	this->actionNum = 0;
	this->linkNum = 0;
}
void Selector::paint(int selector)
{
	for (int i=0;i<this->actionNum;i++)
	{
		if (selector == 1)
		{
			this->SelectorUpLeft[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width/2 - 4
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y);
			this->SelectorUpRight[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width/2 - 4
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height - 8);
			this->SelectorDownLeft[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height/2 - 4);
			this->SelectorDownRight[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width - 8
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height/2 - 4);
		}
		else
		{
			this->SelectorUpLeft[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y);
			this->SelectorUpRight[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width - 8
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y);
			this->SelectorDownLeft[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height - 8);
			this->SelectorDownRight[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width - 8
																		,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height - 8);
		}
		this->SelectorUpLeft[i]->BringToFront();
		this->SelectorUpRight[i]->BringToFront();
		this->SelectorDownLeft[i]->BringToFront();
		this->SelectorDownRight[i]->BringToFront();
	}
	for (int i=this->actionNum;i<this->source->getAMax();i++)
	{
		this->SelectorUpLeft[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorUpRight[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorDownLeft[i]->Location = System::Drawing::Point(722, 513);
		this->SelectorDownRight[i]->Location = System::Drawing::Point(722, 513);
	}
}
int Selector::getActionNum()
{
	return this->actionNum;
}
int Selector::getLinkNum()
{
	return this->linkNum;
}
void Selector::move(int x,int y)
{
	for (int i=0;i<this->actionNum;i++)
	{
		this->source->Actions[this->SelectorAction[i]]->move(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + x, this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + y);
		this->source->Actions[this->SelectorAction[i]]->paint();
		this->SelectorUpLeft[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X
																	,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y);
		this->SelectorUpLeft[i]->BringToFront();
		this->SelectorUpRight[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width - 8
																	,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y);
		this->SelectorUpRight[i]->BringToFront();
		this->SelectorDownLeft[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X
																	,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height - 8);
		this->SelectorDownLeft[i]->BringToFront();
		this->SelectorDownRight[i]->Location = System::Drawing::Point(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width - 8
																	,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height - 8);
		this->SelectorDownRight[i]->BringToFront();
	}
}
void Selector::resize(int x,int y,int w,int h)
{
	for (int i=0;i<this->actionNum;i++)
	{
		this->source->Actions[this->SelectorAction[i]]->resize(this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.X + x
																,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Location.Y + y
																,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Width + w
																,this->source->Actions[this->SelectorAction[i]]->getActionImg()->Size.Height + h);
	}
}
void Selector::setText(System::String^ newText)
{
	for (int i=0;i<this->actionNum;i++)
	{
		this->source->Actions[this->SelectorAction[i]]->setText(newText);
	}
}
void Selector::deleteSelected()
{
	for (int i=0;i<this->linkNum;i++)
	{
		this->source->DeleteLink(this->SelectorLink[i]);
	}
	for (int i=0;i<this->actionNum;i++)
	{
		this->source->DeleteObject(this->SelectorAction[i]);
	}
	null();
}
int Selector::getFirst()
{
	return this->SelectorAction[0];
}
int Selector::getSecond()
{
	return this->SelectorAction[1];
}
int Selector::getResizeTool(System::Object^  object)
{
	for (int i=0;i<this->source->getAMax();i++)
	{
		if (((System::Windows::Forms::PictureBox^)object) == this->SelectorUpLeft[i])
		{
			return 1;
		}
		if (((System::Windows::Forms::PictureBox^)object) == this->SelectorUpRight[i])
		{
			return 2;
		}
		if (((System::Windows::Forms::PictureBox^)object) == this->SelectorDownLeft[i])
		{
			return 3;
		}
		if (((System::Windows::Forms::PictureBox^)object) == this->SelectorDownRight[i])
		{
			return 4;
		}
	}
	return 0;
}